#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "src/socket_client_api.h"
#include "src/socket_server_api.h"

/****************************服务端*************************************/
static void client_onLogin(socket_server_t *server, client_data_t client)
{
	printf("有新客户端登录(socket_fd=%d, ip=%s, port=%d)\n", client.socket_fd, client.ip, client.port);
	printf("在线客户端总数:%d\n", server->client_list.count);
	print_socket_client_list(&server->client_list);
	printf("\n\n");
}
static void client_onMessage(socket_server_t *server, client_data_t client, void *buf, int len)
{
	printf("收到客户端消息(socket_fd=%d ip=%s port=%d)：len=%d, buf=%s\n", client.socket_fd, client.ip, client.port, len, (char *)buf);
#if 0
	char send_msg[128] = "hello";
	printf("返回消息给客户端：%s\n", send_msg);
	send(client.socket_fd, send_msg, strlen(send_msg), 0);//发送数据
	printf("\n\n");
#endif    
}
static void client_onExit(socket_server_t *server, client_data_t client)
{
	printf("有客户端退出(socket_fd=%d, ip=%s, port=%d)\n", client.socket_fd, client.ip, client.port);
	printf("剩余客户端总数:%d\n", server->client_list.count);
	print_socket_client_list(&server->client_list);
	printf("\n\n");
}

void server_loop(void)
{
    socket_server_t server1 = {
        .type = 0,
        .socket_type = SOCK_STREAM,
        .socket_protocol = 0,
        .broadcast_flag = 0,
        .ip = "0.0.0.0",
        .bind_port = 6666,
        .max_listen_backlog = 32,
        .max_client_online_num = 128,
        .rcv_buf_size = 0,
    
        .client_onLogin = client_onLogin,
        .client_onMessage = client_onMessage,
        .client_onExit = client_onExit
    };
        
	//启动socket服务端
	start_socket_server(&server1);
    
    while(1) {
	    sleep(10);
#if 1
        //给每个客户端发送消息
        client_node_t* current = server1.client_list.head;
        while (current != NULL) {
            client_data_t client = current->data;
            send(client.socket_fd, "hello", strlen("hello"), 0);//发送数据
            current = current->next;
        }
#endif
    }
    
	//关闭socket服务端
	close_socket_server(&server1);
}


/****************************客户端*************************************/
void on_message(int fd, void *buf, int len, void *arg)
{
	unsigned char *ptr = (unsigned char *)buf;
    //for (int i = 0; i < len; i++) {
     //   printf("%02x ", ptr[i]);
    //}
    //printf("\n");
    printf("收到服务端消息(len=%d)：%s\n", len, ptr);
}

void client_loop(void)
{
	socket_client_t client1 = {
        .remote_ip = (char *)"172.21.81.226",
        .remote_port = 6666,
		.socket_type = SOCK_STREAM,
		.socket_protocol = 0,
		.broadcast_flag = 0,
		.bind_port = 0,
		.rcv_buf_size = 0,
		
		.on_connect = NULL,
		.on_disconnect = NULL,
		.on_message = on_message
	};

    start_socket_client(&client1);
	char str[64] = {0};
	int i = 0;
    while(1) {
        sleep(5);
		if (client1.connect_flag) {
            i++;
            sprintf(str, "hello %d", i);
            printf("发送消息到服务端：%s\n", str);
            send(client1.socket_fd, str, strlen(str), 0);
            //socket_sendn(client1.socket_fd, str, strlen(str));
        }
    }
    close_socket_client(&client1);
}


int main(int argc, char *argv[])
{
    printf("choose: 1 -- tcp client; 2 -- tcp server\n");
    if (1 >= argc) {
        return 0;
    }
    
    int mode = atoi(argv[1]);
    
    if (1 == mode) {
        client_loop();
    } else {
        server_loop();
    }
	
	return 0;
}
